﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GenericLibrariesToolKit;
using Model;
using Utils.Enumeration;
using Utils;

namespace BLL.Users.Impl
{
    /// <summary>
    /// 用户业务接口实现
    /// </summary>
    public class UserBOImpl : DaoSupport<User>,UserBO
    {
        public string Login(User user)
        {
            User userObj = Find(user.UserId);
            string MD5UserPassword = EncryptUtil.Encode(user.UserPassword);
            //用户不存在
            if (userObj == null)
            {
                return "User not exist";
            }
            //用户密码错误
            else if (userObj.UserPassword != MD5UserPassword)
            {
                return "Wrong Password";
            }
            //用户未通过审核
            else if (userObj.IsAuditing == 0)
            {
                return "User not confirmed";
            }
            //用户已过期
            else if (userObj.Visible == 0)
            {
                return "User not avaliable";
            }
            //验证成功
            else
            {
                return "Login success";
            }
        }

        public string UpdatePassword(User user, string newPassword)
        {
            bool result = false;
            User userObj = Find(user.UserId);
            //用户不存在
            if (userObj == null)
            {
                return "User not exist";
            }
            string oldPassword = EncryptUtil.Encode(user.UserPassword);
            if (userObj.UserPassword != oldPassword)
            {
                return "Wrong old Password";
            }
            else
            {
                userObj.UserPassword = EncryptUtil.Encode(newPassword);
                result = Update(userObj);
                if (result)
                {
                    return "Update success";
                }
                else
                {
                    return "Update fail";
                }
            }
        }

        public string setPassword(User user, string newPassword)
       {
              bool result = false;
             User userObj = Find(user.UserId);
             userObj.UserPassword = EncryptUtil.Encode(newPassword);
             result = Update(userObj);
             if (result)
             {
                 return "reset success";
             }
             else
             {
                 return "reset fail";
             }
       }

        public string PasswordRecovery(User user)
        {
            //根据用户id查询用户对象
            User userObj = Find(user.UserId);
            //密保问题正确
            if (userObj.SecurityQuestion == user.SecurityQuestion)
            {
                //密保答案正确
                if (userObj.SecurityAnswer == user.SecurityAnswer)
                {
                    return "";
                }
                //密保答案错误
                else
                {
                    return null;
                }

            }
            //密保问题错误
            else
            {
                return null;
            }
        }

        public bool Delete(string userId)
        {
            //根据用户id获取用户对象
            User userObj = Find(userId);
            if (userObj == null)
            {
                return false;
            }
            //将获取对象的Visible属性设置为不可见
            userObj.Visible = 0;
            //重新更新该对象
            bool result = Update(userObj);

            return result;
        }

    }
}
